const { mongoose, db } = require('../utils/mongoose')
let schema = mongoose.Schema({
    id:{type:String,required:true},
    englishTitle:{type:String,required:true},
    chineseTitle:{type:String,required:true},
    subTitle:{type:String,required:true},
    paragraph:{type:Number,required:true},
    content:{type:Array,required:true},
    author:{type:String,required:true},
    sourceUrl:{type:String,required:true},
})

let Collection = mongoose.model('paragraphs', schema)


//新增
const creteParagraph=(data)=>{
    let model = new Collection(data)
    return model.save()
        .then(res => res)
        .catch(err => {
            console.log(err);
            return false;
        })
}
//查询
// .sort({'date':-1})//按插入时间倒序排列
// date:{$gte: sDate, $lte: eDate}
const findParagraphs=(data)=>{
    return Collection.find(data).sort({'_id':-1})
        .then(res=>res)
        .catch(err=>{
            console.log(err);
            return false;
        })
}

// 删
let delParagraph = (query = {}) => {
    return Collection.deleteOne(query)
        .then(res=>res)
        .catch(err=>{
            console.log(err)
            return false;
        })
}

//操作
const updateParagraph = (query, updated) => {
    console.log(updated)
    return Collection.updateOne(query, {$set:{"content":updated}})
      .then((res) => res)
      .catch((err) => {
        return false;
      });
  };
//操作
// const editToDoList=(query,updated)=>{
    
//     return Collection.updateOne(query,updated)
//         .then(res=>res)
//         .catch(err=>{
//             console.log(err);
//             return false;
//         })
// }
module.exports={
    creteParagraph,
    findParagraphs,
    delParagraph,
    updateParagraph
}